package com.lagou.service;

import com.lagou.client.RpcRequest;
import com.lagou.client.RpcResponse;
import com.lagou.handler.NettyServerHandler;
import com.lagou.handler.UserServerHandler;
import com.lagou.util.RpcDecoder;
import com.lagou.util.RpcEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class UserServiceImpl implements UserService {

    public String sayHello(String word) {
        System.out.println("调用成功 "+word);
        return "调用成功 "+word;
    }

    //hostName:ip地址  port:端口号
    public static void startServer(String hostName,int port) throws InterruptedException {

        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
try {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
//            pipeline.addLast(new StringEncoder());
//            pipeline.addLast(new StringDecoder());
            pipeline.addLast(new NettyServerHandler());

        }
    });
    ChannelFuture channelFuture = serverBootstrap.bind(hostName, port).addListener(future -> {
        if (future.isSuccess()) {
            System.out.println("服务端启动成功，监听端口：" + port);
        }
    });
    channelFuture.channel().closeFuture().sync();
}catch(Exception e){
    System.out.println("Exception：" + e);
}finally {
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
}


    }

}
